var gulp = require("gulp"),
    clean = require('gulp-clean'),
    browserify = require('browserify'),
    reactify = require('reactify'),
    source = require('vinyl-source-stream'),
    rename = require('gulp-rename'),
    babel = require("gulp-babel"),
    uglify = require('gulp-uglify'),
    minimist = require('minimist'),
    argv = require('minimist')(process.argv.slice(2));

gulp.task('es6TurnEs5', function (callback) {
    gulp.src(argv.filePath + '\\'+ argv.fileName)// ES6 源码存放的路径
        .pipe(babel({
            presets: ['es2015', "stage-0", "react"]
        }))
        .pipe(uglify())
        .pipe(gulp.dest(argv.filePath))//转换成 ES5 存放的路径
        .on('end', function () {
            console.log('uglify has benn comleted');
            callback()
        });
});

//gulp将ES6转ES5
gulp.task('es6TurnEs5NoCompact', function (callback) {
    gulp.src(argv.filePath + '\\'+ argv.fileName)// ES6 源码存放的路径
        .pipe(babel({
            presets: ['es2015', "stage-0","react"]
        }))
        .pipe(gulp.dest(argv.filePath))//转换成 ES5 存放的路径
        .on('end', function () {
            console.log('uglify has benn comleted');
            callback()
        });
});

//只用gulp进行压缩js
gulp.task('Compact', function (callback) {
    gulp.src(argv.filePath +'\\'+ argv.fileName)// ES6 源码存放的路径
        .pipe(uglify())
        .pipe(rename({suffix:'.min'}))
        .pipe(gulp.dest(argv.filePath))//转换成 ES5 存放的路径
        .on('end', function () {
            console.log('uglify has benn comleted '+argv.filePath + '\\'+argv.fileName);
            callback()
        });
});

gulp.task('jsxNoCompact', ['es6TurnEs5NoCompact'], function () {
    browserify(argv.filePath + '/dist/' + argv.targetName)
        .transform(reactify)
        .bundle()
        .pipe(source(argv.targetName))
        .pipe(gulp.dest(argv.filePath))
        .on('end', function () {
            gulp.src(argv.filePath)
                .pipe(clean());
        });
});

gulp.task('jsx', ['es6TurnEs5'], function () {
    browserify(argv.filePath + '/dist/' + argv.targetName)
        .transform(reactify)
        .bundle()
        .pipe(source(argv.targetName))
        .pipe(gulp.dest(argv.filePath))
        .on('end', function () {
            gulp.src(argv.filePath + '/dist/')
                .pipe(clean());
        });
});